<template>
	<view class="page-container">
		<!-- 卡片容器 -->
		<view class="card">
			<view class="title">新用户注册</view>

			<view class="form">
				<!-- 用户名 -->
				<view class="input-item">
					<input v-model="userName" type="text" placeholder="请输入用户名" class="input" />
				</view>

				<!-- 密码 -->
				<view class="input-item password-item">
					<input v-model="firstPassword" :type="showFirstPassword ? 'text' : 'password'" placeholder="请输入密码"
						class="input" @focus="showFirstPasswordToggle = true" @blur="handleFirstPasswordBlur" />
					<text class="password-toggle" v-show="showFirstPasswordToggle" @mousedown="handleToggleMouseDown"
						@click="toggleFirstPassword">
						{{ showFirstPassword ? '👁️' : '🔒' }}
					</text>
				</view>

				<!-- 确认密码 -->
				<view class="input-item password-item">
					<input v-model="secondPassword" :type="showSecondPassword ? 'text' : 'password'"
						placeholder="请再次输入密码" class="input" @focus="showSecondPasswordToggle = true"
						@blur="handleSecondPasswordBlur" />
					<text class="password-toggle" v-show="showSecondPasswordToggle" @mousedown="handleToggleMouseDown"
						@click="toggleSecondPassword">
						{{ showSecondPassword ? '👁️' : '🔒' }}
					</text>
				</view>

				<!-- 错误提示 -->
				<text class="error" v-if="error">{{ error }}</text>

				<!-- 注册按钮 -->
				<button class="button register-btn" :loading="isLoading" @click="onRegisterButtonClicked">
					注册
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	let tag = 'register';
	export default {
		data() {
			return {
				userName: "",
				firstPassword: "",
				secondPassword: "",
				isLoading: false,
				error: "",
				showFirstPassword: false,
				showSecondPassword: false,
				showFirstPasswordToggle: false,
				showSecondPasswordToggle: false,
				firstPasswordBlurTimer: null,
				secondPasswordBlurTimer: null
			}
		},
		methods: {
			onRegisterButtonClicked() {
				if (this.isLoading) return;

				this.$log.info(tag, "onRegisterButtonClicked");

				if (!this.userName || !this.firstPassword || !this.secondPassword) {
					this.error = "用户名或密码为空";
					uni.showToast({
						icon: 'none',
						title: "用户名或密码为空",
						duration: 1500
					});
					return;
				}

				if (this.firstPassword !== this.secondPassword) {
					this.error = "密码不一致";
					uni.showToast({
						icon: 'none',
						title: "密码不一致",
						duration: 1500
					});
					this.firstPassword = "";
					this.secondPassword = "";
					return;
				}

				this.isLoading = true;
				this.getCode(this.userName, this.firstPassword);
			},

			getCode(userName, password) {
				this.$log.info(tag, "getCode");
				uni.request({
					url: `${this.constant.BASE_URL}/api/code`,
					method: 'GET',
					data: {
						password: password,
						username: userName
					},
					success: (res) => {
						this.$log.info(tag, "getCode success");
						this.$log.info(tag, JSON.stringify(res.data));
						this.$log.info(tag, res.data.valid_code);
						this.register(userName, password, res.data.valid_code)
					},
					fail: (err) => {
						this.isLoading = false;
						this.error = "获取验证码失败";
						this.$log.error(tag, "getCode fail: " + err.errMsg);
					}
				});
			},

			register(userName, password, valid_code) {
				this.$log.info(tag, "register");
				uni.request({
					url: `${this.constant.BASE_URL}/api/register`,
					method: 'POST',
					data: {
						password: password,
						username: userName,
						code: valid_code
					},
					success: (res) => {
						this.isLoading = false;
						this.$log.info(tag, "register success");
						this.$log.info(tag, JSON.stringify(res.data));
						if (res.data.code == 200) {
							this.$log.info(tag, userName + "注册成功");
							uni.showToast({
								icon: 'success',
								title: res.data.msg || "注册成功",
								duration: 1500
							});
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 1500);
						} else {
							this.error = res.data.msg || "注册失败";
							uni.showToast({
								icon: 'none',
								title: res.data.msg || "注册失败",
								duration: 1500
							});
						}
					},
					fail: (err) => {
						this.isLoading = false;
						this.error = "注册请求失败";
						this.$log.error(tag, "register fail: " + err.errMsg);
						uni.showToast({
							icon: 'none',
							title: "注册请求失败",
							duration: 1500
						});
					}
				})
			},
			toggleFirstPassword() {
				this.showFirstPassword = !this.showFirstPassword;
			},
			toggleSecondPassword() {
				this.showSecondPassword = !this.showSecondPassword;
			},
			handleToggleMouseDown(e) {
				e.preventDefault();
			},
			handleFirstPasswordBlur() {
				this.firstPasswordBlurTimer = setTimeout(() => {
					this.showFirstPasswordToggle = false;
				}, 150);
			},
			handleSecondPasswordBlur() {
				this.secondPasswordBlurTimer = setTimeout(() => {
					this.showSecondPasswordToggle = false;
				}, 150);
			}
		},
		beforeDestroy() {
			if (this.firstPasswordBlurTimer) {
				clearTimeout(this.firstPasswordBlurTimer);
			}
			if (this.secondPasswordBlurTimer) {
				clearTimeout(this.secondPasswordBlurTimer);
			}
		}
	}
</script>

<style scoped>
	* {
		box-sizing: border-box;
		margin: 0;
		padding: 0;
	}

	.page-container {
		padding: 60rpx 20rpx;
		min-height: 100vh;
		background-color: #f0f2f5;
		display: flex;
		justify-content: center;
		align-items: flex-start;
	}

	.card {
		width: 100%;
		max-width: 500rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 40rpx 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
	}

	.title {
		text-align: center;
		font-size: 38rpx;
		font-weight: 600;
		color: #1677ff;
		margin-bottom: 40rpx;
	}

	.form {
		width: 100%;
	}

	.input-item {
		margin-bottom: 40rpx;
		position: relative;
	}

	.password-item {
		display: flex;
		align-items: center;
	}

	.input {
		width: 100%;
		height: 80rpx;
		font-size: 28rpx;
		color: #333;
		padding: 18rpx 20rpx;
		padding-right: 80rpx;
		border: 1px solid #e0e0e0;
		border-radius: 10rpx;
		background-color: #fff;
		transition: border-color 0.3s ease;
	}

	.input:focus {
		border-color: #007aff;
		outline: none;
		box-shadow: 0 0 0 4rpx rgba(0, 122, 255, 0.1);
	}

	.password-toggle {
		position: absolute;
		right: 0;
		color: #007aff;
		font-size: 36rpx;
		padding: 20rpx;
		cursor: pointer;
		user-select: none;
	}

	.error {
		display: block;
		color: #ff4d4f;
		font-size: 26rpx;
		margin: -20rpx 0 40rpx;
		height: 30rpx;
	}

	.button {
		width: 100%;
		font-size: 32rpx;
		font-weight: 500;
		border-radius: 10rpx;
		margin-bottom: 15rpx;
		transition: all 0.2s ease;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: none;
	}

	.register-btn {
		background-color: #007aff;
		color: #fff;
		box-shadow: 0 2rpx 8rpx rgba(0, 122, 255, 0.2);
	}

	.register-btn:active {
		background-color: #0066cc;
		transform: scale(0.98);
		box-shadow: 0 1rpx 4rpx rgba(0, 122, 255, 0.15);
	}
</style>